package net.lainiao.leetcode;

import java.io.File;
import java.nio.file.Paths;
import java.util.Stack;

/**
 * Created by Administrator on 2018/3/9.
 */
public class RenameGuns {
    static Stack<File> stack=new Stack<>();
    static File noFile=new File("D:\\MyGit\\learn\\guns-admin");
    public static void main(String[] args) {
        String path="D:\\MyGit\\learn";
        File file=new File(path);
        eachFile(file);
        int count=stack.size();
        for (int i = 0; i <count; i++) {
            File dirFile=stack.pop();
            if(dirFile.getAbsolutePath().equals(noFile.getAbsolutePath())){
                System.out.println("存在这个文件");
            }
            renameToGuns(dirFile);
        }
    }

    private static void eachFile(File file) {
        if(file.isDirectory()){
            stack.push(file);
            for(File fileChild:file.listFiles()){
                eachFile(fileChild);
            }
        }
        else{
            renameToGuns(file);
        }
    }

    private static void renameToGuns(File file) {
        String fileName=file.getName();
        if(fileName.startsWith(".")){
            return ;
        }
        else if(fileName.indexOf("Guns")>-1){
            String newFileName= Paths.get(file.getParent(),fileName.replaceAll("Guns","Learn")).toString();
            System.out.println(newFileName);
            file.renameTo(new File(newFileName));
        }
        else if(fileName.indexOf("guns")>-1){
            String newFileName= Paths.get(file.getParent(),fileName.replaceAll("guns","learn")).toString();
            System.out.println(newFileName);
            file.renameTo(new File(newFileName));
        }else{
            System.out.println(fileName);
        }
    }
}
